package com.briup.web.listener;

import com.briup.bean.Book;
import com.briup.bean.Category;
import com.briup.service.IBookService;
import com.briup.service.ICategoryService;
import com.briup.service.impl.BookServiceImpl;
import com.briup.service.impl.CategoryServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

/**
 * 监听器：项目启动的时候创建
 */
@WebListener
public class IndexListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //测试监听器
        System.out.println("servletContest创建了");

        //1、获取servletContext对象
        ServletContext servletContext = sce.getServletContext();
        //实现类对象
        ICategoryService categoryService = new CategoryServiceImpl();
        IBookService bookService = new BookServiceImpl();

        //2、把查询结果存到最大的作用域当中
        //xxx.setAttribute("CategoryList", ?);
        //查询所有分类信息
        List<Category> allCategorys = categoryService.findAllCategorys();
        servletContext.setAttribute("CategoryList", allCategorys);

        //查询所有图书信息
        List<Book> allBooks = bookService.findAllBooks();
        //存入对应作用域
        servletContext.setAttribute("BookList", allBooks);


    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
